package com.hp.web.controller.system;

import com.hp.common.annotation.Log;
import com.hp.common.core.controller.BaseController;
import com.hp.common.core.domain.AjaxResult;
import com.hp.common.core.page.TableDataInfo;
import com.hp.common.enums.BusinessType;
import com.hp.common.utils.poi.ExcelUtil;
import com.hp.system.domain.*;
import com.hp.system.service.CarTypeService;
import com.hp.system.service.DestbationCityService;
import com.hp.system.service.IdentifyInfoServce;
import com.hp.system.service.PassInfoService;
import com.hp.system.utils.JSONInfoUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;

@Controller
@RequestMapping("/system/passinfo")
public class PassInfoController extends BaseController {
    @Autowired
    private PassInfoService passInfoService;
    @Autowired
    private DestbationCityService destbationCityService;
    @Autowired
    private IdentifyInfoServce identifyInfoServce;
    @Autowired
    private CarTypeService carTypeService;

    private String prefix = "system/passinfo";

    @RequiresPermissions("system:passinfo:view")
    @GetMapping()
    public String passinfo(){
        return prefix+"/passinfo";
    }

    @RequiresPermissions("system:passinfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PassInfo passInfo){
        startPage();
        List<PassInfo> list = passInfoService.selectByPassInfo(passInfo);
        return getDataTable(list);
    }

    @RequiresPermissions("system:passinfo:edit")
    @GetMapping("/edit/{passid}")
    public String edit(@PathVariable("passid") Integer passid, ModelMap mmap){
        PassInfo passInfo = passInfoService.showPassInfoById(passid);
        mmap.put("passInfo",passInfo);
        return prefix+"/edit";
    }

    @RequiresPermissions("system:passinfo:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult updateInfo(PassInfo passInfo) throws Exception {
        return toAjax(passInfoService.updatePassInfo(passInfo));
    }

    @RequiresPermissions("system:passinfo:detail")
    @GetMapping("/detail/{passid}")
    public String detail(@PathVariable("passid") Integer passid, ModelMap mmap)
    {
        mmap.put("passInfo", passInfoService.showPassInfoById(passid));
        return prefix + "/detail";
    }

    @Log(title = "进出记录", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:passinfo:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PassInfo passInfo)
    {
        List<PassInfo> list = passInfoService.getPassInfoSheet(passInfo);
        ExcelUtil<PassInfo> util = new ExcelUtil<PassInfo>(PassInfo.class);
        return util.exportExcel(list, "进出记录");
    }

    @PostMapping("/dealInfo")
    @ResponseBody
    public AjaxResult dealPassInfo(Integer passid) throws Exception {
        PassInfo passInfo = passInfoService.getPassInfoByid(passid);
        passInfo.setDealtime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(JSONInfoUtil.getTime()));
        passInfo.setIsdeal(new Byte("1"));
        return toAjax(passInfoService.updatePassInfo(passInfo));
    }

    @PostMapping("/getcount")
    @ResponseBody
    public String getcount(){
        return passInfoService.getCountByIsdeal();
    }

    @RequiresPermissions("system:passinfo:city")
    @PostMapping("/city")
    @ResponseBody
    public List<DestbationCity> getCity(Integer destId){
        List<DestbationCity> list = destbationCityService.showDestCity(destId);
        return list;
    }


    @RequiresPermissions("system:passinfo:type")
    @PostMapping("/type")
    @ResponseBody
    public List<CarType> getType(Integer belongid){
        List<CarType> list = carTypeService.showType(belongid);
        return list;
    }
}
